package com.liukai.exam.service.impl;

import com.liukai.exam.mapper.*;
import com.liukai.exam.pojo.BigQuestion;
import com.liukai.exam.pojo.ExamManage;
import com.liukai.exam.service.IExamManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liukai.exam.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 试卷管理 服务实现类
 * </p>
 *
 * @author liu kai
 * @since 2021-02-01
 */
@Service
public class ExamManageServiceImpl extends ServiceImpl<ExamManageMapper, ExamManage> implements IExamManageService {

    @Autowired
    private ExamManageMapper examManageMapper;

    @Autowired
    private ApplicationQuestionMapper applicationQuestionMapper;

    @Autowired
    private JudgeQuestionMapper judgeQuestionMapper;

    @Autowired
    private MoreChoiceQuestionMapper moreChoiceQuestionMapper;

    @Autowired
    private OneChoiceQuestionMapper oneChoiceQuestionMapper;

    @Autowired
    private FillQuestionMapper fillQuestionMapper;

    @Autowired
    private BigQuestionMapper bigQuestionMapper;

    /**
     * 新增试卷信息
     *
     * @param examManage
     * @return
     */
    @Override
    public int addExamManage(ExamManage examManage) {

        Long teacherId = examManage.getTeacherId();
        Integer classId = examManage.getClassId();

        // 根据教师编号和班级编号获取所有的试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage manage : examManageList) {
            // 如果新增的试卷的名称在该教师编号与班级编号下已存在，则不新增
            if (manage.getExamName().equals(examManage.getExamName())) {
                // 返回试卷信息新增失败，试卷名称已存在
                return Constant.EXAM_MANAGE_ADD_FILED_NAME_EXIST;
            }
        }

        return examManageMapper.addExamManage(examManage);
    }

    /**
     * 根据教师编号和班级编号获取所有的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findExamNameById(Long teacherId, Integer classId) {
        return examManageMapper.findExamNameById(teacherId, classId);
    }

    /**
     * 根据教师编号和班级编号和试卷名称获取所有的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public ExamManage findExamNameByName(Long teacherId, Integer classId, String examName) {
        return examManageMapper.findExamNameByName(teacherId, classId, examName);
    }

    /**
     * 根据账号信息查询未完成创建的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findNoDoneCreateExam(Long teacherId, Integer classId) {
//        System.out.println("findNoDoneCreateExam");
        List<ExamManage> noDoneCreateExamList = new ArrayList<>();
        // 1.获取该账号信息下的所有试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage examManage : examManageList) {
//            System.out.println(examManage.toString());
            // 2.获取每一套试卷的examId
            Long examId = examManage.getExamId();
            // 3.根据examId获取试卷大题类型
            List<BigQuestion> bigQuestionList = bigQuestionMapper.findBigQueById(examId);
            // 4.判断试卷大题类型是否为空，为空不获取未完成创建列表，跳出当前循环
            if (bigQuestionList.size() == 0)
                continue;
            // 5.判断试卷编号为examId的试卷是否完成创建
            boolean doneCreate = isDoneCreate(bigQuestionList, teacherId, classId);
            if (doneCreate == false)
                // 6.将未完成创建的试卷添加到列表中
                noDoneCreateExamList.add(examManage);
        }

        return noDoneCreateExamList;
    }

    /**
     * 根据账号信息查询已完成创建的试卷信息
     *
     * @param teacherId
     * @param classId
     * @return
     */
    @Override
    public List<ExamManage> findDoneCreateExam(Long teacherId, Integer classId) {

//        System.out.println("findDoneCreateExam");
        List<ExamManage> doneCreateExamList = new ArrayList<>();
        // 1.获取该账号信息下的所有试卷信息
        List<ExamManage> examManageList = examManageMapper.findExamNameById(teacherId, classId);
        for (ExamManage examManage : examManageList) {
//            System.out.println(examManage.toString());
            // 2.获取每一套试卷的examId
            Long examId = examManage.getExamId();
            // 3.根据examId获取试卷大题类型
            List<BigQuestion> bigQuestionList = bigQuestionMapper.findBigQueById(examId);
            // 4.判断试卷大题类型是否为空，为空不获取未完成创建列表，跳出当前循环
            if (bigQuestionList.size() == 0)
                continue;
            // 5.判断试卷编号为examId的试卷是否完成创建
            boolean doneCreate = isDoneCreate(bigQuestionList, teacherId, classId);
            if (doneCreate == true)
                // 6.将已经完成创建的试卷添加到列表中
                doneCreateExamList.add(examManage);
        }

        return doneCreateExamList;
    }

    /**
     * 判断试卷是否完成创建
     * @param bigQuestionList
     * @param teacherId
     * @param classId
     * @return
     */
    private boolean isDoneCreate(List<BigQuestion> bigQuestionList, Long teacherId, Integer classId) {

        for (BigQuestion bigQuestion : bigQuestionList) {
            Integer bigQuestionIndex = bigQuestion.getBigQuestionIndex();
            switch (bigQuestionIndex) {
                case Constant.ONE_CHOICE:
                    int oneChoiceCount = oneChoiceQuestionMapper.oneChoiceQuestionById(teacherId, classId);
                    if (oneChoiceCount == 0) {
                        return false;
                    }
                    break;
                case Constant.MORE_CHOICE:
                    int moreChoiceCount = moreChoiceQuestionMapper.moreChoiceQuestionById(teacherId, classId);
                    if (moreChoiceCount == 0) {
                        return false;
                    }
                    break;
                case Constant.JUDGE:
                    int judgeCount = judgeQuestionMapper.judgeQuestionById(teacherId, classId);
                    if (judgeCount == 0) {
                        return false;
                    }
                    break;
                case Constant.FILL:
                    int fillCount = fillQuestionMapper.fillQuestionById(teacherId, classId);
                    if (fillCount == 0) {
                        return false;
                    }
                    break;
                case Constant.APPLICATION:
                    int ApplicationCount = applicationQuestionMapper.applicationQuestionById(teacherId, classId);
                    if (ApplicationCount == 0) {
                        return false;
                    }
                    break;
                default:
                break;
            }
        }
        return true;
    }

}
